/**
 * 
 */
package com.easidea.tim.service.commodity;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.commodity.ProductDao;
import com.easidea.tim.entity.commodity.Product;

/**
 * @author Harrison
 *
 */
public class ProductService extends EntityService<Product,ProductDao> {
	
	private ProductDao productDao;
	private static final Logger log = Logger.getLogger(ProductService.class);
	public static final String PRODUCT_COLOR_STYLE_R = "R"; //挂板的色号
	
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	public List<Product> filteProductByName(String name){
		return productDao.filteProductByName(name);
	}
	
	
	/**
	 * 保存最低限价
	 * @param id
	 * @param price
	 * @author Harrison
	 * @return
	 */
	public boolean savePrice(Long id, Double price){
		boolean rst = false;
		if(id != null && price != null){
			Product pdt = productDao.get(id);
			if(pdt != null && pdt.getName() != null){
				pdt.setBasicPrice(price.floatValue());
				rst = true;
			}
		}
		return rst;
	}
	
	
	public int bulkSavePriceByName(String name, Double price){
		if(name != null && StringUtils.isNotBlank(name.trim()) && price != null){
			Object[] values = {price.floatValue(),name.trim()};
			int counts = productDao.getHibernateTemplate().bulkUpdate("update com.easidea.tim.entity.commodity.Product p set p.basicPrice = ? where p.deleted = 0 and p.name = ?", values);
			return counts;
		}
		return -1;
	}
	
	/**
	 * 根据品名和色号查找布品
	 * @param name
	 * @param color
	 * @return
	 */
	public Product searchProductByNameAndColor(String name,String color){
		return productDao.getProductByColorAndName(name, color);
	}
	
	/**
	 * 
	 * @param pName
	 * @param color
	 * @param price
	 * @param userName
	 * @return
	 */
	public boolean saveProduct(String pName, String color, Double price, String userName){
		if( price != null && StringUtils.isNotBlank(pName) && StringUtils.isNotBlank(color)){
			Criteria cte = productDao.createUnDeletedCriteria();
			cte.add(Restrictions.eq("name", pName.trim().toUpperCase()));
			cte.add(Restrictions.eq("colorCode", color.trim().toUpperCase()));
			cte.setFetchSize(1);
			Product p = (Product)cte.uniqueResult();
			if(p != null){
				log.error("Duplicated product name and color: " + pName + " : " + color);
				return false;
			}else{
				p = new Product();
				Product productTmp = productDao.getProductBasicPrice(pName);
				
				if(productTmp !=null){
					p.setBasicPrice(productTmp.getBasicPrice());
				}else{
					p.setBasicPrice(Float.valueOf("0"));
				}
				
				
				
				p.setName(pName.trim().toUpperCase());
				p.setColorCode(color.trim().toUpperCase());
				p.setCreator(userName);
				p.setModifier(userName);
				p.setCreateTime(new Date());
				p.setModifyTime(new Date());
				p.setDummyFlag(false);
				productDao.save(p);
				
				// 判断当前产品是否包含有挂板的产品数据
				Criteria cteDummy = productDao.createUnDeletedCriteria();
				cteDummy.add(Restrictions.eq("name", pName));
				cteDummy.add(Restrictions.eq("dummyFlag", true));
				cteDummy.setFetchSize(1);
				Product dummySearcedProduct = (Product)cteDummy.uniqueResult();
				if(dummySearcedProduct == null){
					Product dummyProduct = new Product();
					dummyProduct.setName(pName.trim().toUpperCase());
					dummyProduct.setColorCode(this.PRODUCT_COLOR_STYLE_R);
					dummyProduct.setBasicPrice(price.floatValue());
					dummyProduct.setCreator(userName);
					dummyProduct.setModifier(userName);
					dummyProduct.setCreateTime(new Date());
					dummyProduct.setModifyTime(new Date());
					dummyProduct.setDummyFlag(true);
					productDao.save(dummyProduct);
					//保存挂板数据
				}
				
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 获取产品价格列表
	 * 因为客户提出对于价格列表不显示colorCode，所以屏蔽了projection数据的处理，修改了该方法返回的数据内容
	 * 
	 * @return
	 * 
	 */
	public List<Object[]> getPricedProductList(String name , String colorCode){
		Criteria cte = productDao.createUnDeletedCriteria();
		cte.add(Restrictions.eq("dummyFlag",false));
		if(StringUtils.isNotBlank(name)){
			cte.add(Restrictions.eq("name", name));
		}
		if(StringUtils.isNotBlank(colorCode)){
			cte.add(Restrictions.eq("colorCode", colorCode));
		}
		
		ProjectionList pList = Projections.projectionList();
		pList.add(Projections.property("id"));
		pList.add(Projections.groupProperty("name"));
		pList.add(Projections.property("basicPrice"));
//		pList.add(Projections.property("colorCode"));
		cte.setProjection(pList);
		cte.addOrder(Order.asc("id"));
//		List<Product> pdtList = (List<Product>)cte.list();
		List<Object[]> pdtList = cte.list();
		return pdtList;
	}
	
	
	/* (non-Javadoc)
	 * @see org.springside.core.service.EntityService#getEntityDao()
	 */
	@Override
	public ProductDao getEntityDao() {
		return productDao;
	}

	/**
	 * @return the productDao
	 */
	public ProductDao getProductDao() {
		return productDao;
	}

	/**
	 * @param productDao the productDao to set
	 */
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
}
